//go:generate goversioninfo
package main

import (
	"distcompiler/common"
	"distcompiler/network"
	"os/exec"
	"syscall"

	"github.com/energye/systray"
)

func main() {
	kernel32 := syscall.NewLazyDLL("kernel32.dll")
	user32 := syscall.NewLazyDLL("user32.dll")
	consoleHandle, _, _ := kernel32.NewProc("GetConsoleWindow").Call()
	user32.NewProc("ShowWindowAsync").Call(consoleHandle, 0)
	sysMenu, _, _ := user32.NewProc("GetSystemMenu").Call(consoleHandle, 0)
	user32.NewProc("DeleteMenu").Call(sysMenu, 0xf060, 0)

	network.Port = 9000
	db := common.New()
	defer db.Close()

	//gin.SetMode(gin.ReleaseMode)
	go network.Discovery()
	go network.TcpRun()
	go network.HttpRun()
	go network.SendBroadCastMessage()

	systray.Run(func() {
		ico, _ := network.EmbedFs.ReadFile("dist/favicon.ico")
		systray.SetTitle("App")
		systray.SetIcon(ico)
		systray.SetTooltip("App Test")
		show := systray.AddMenuItem("Show", "Show the console")
		hide := systray.AddMenuItem("Hide", "Hide the console")
		systray.AddSeparator()
		open := systray.AddMenuItem("VChat", "Chat with others")
		quit := systray.AddMenuItem("Quit", "Quit application")
		systray.SetOnDClick(func(menu systray.IMenu) {
			exec.Command("cmd.exe", "/c", "start http://localhost:9001").Run()
		})
		systray.SetOnRClick(func(menu systray.IMenu) {
			menu.ShowMenu()
		})
		open.Click(func() {
			exec.Command("cmd.exe", "/c", "start http://localhost:9001").Run()
		})
		hide.Click(func() {
			user32.NewProc("ShowWindowAsync").Call(consoleHandle, 0)
		})
		show.Click(func() {
			user32.NewProc("ShowWindowAsync").Call(consoleHandle, 5)
		})
		quit.Click(func() {
			systray.Quit()
		})
	}, nil)
}
